import { execute } from "../../database/connector";

import { AnnouncementQueries } from "../queries/announcement.query";
import { IAnnouncement } from "../../models/announcement.model";

/**
 * gets active announcement
 */
export const getAnnouncements = async () => {
  return execute<IAnnouncement[]>(AnnouncementQueries.GetAnnouncements, []);
};

/**
 * gets a announcement based on id provided
 */
export const getAnnouncementById = async (id: IAnnouncement['id']) => {
  return execute<IAnnouncement>(AnnouncementQueries.GetAnnouncementById, [id]);
};

/**
 * adds a new active announcement record
 */
export const insertAnnouncement = async (announcement: IAnnouncement) => {
  const result = await execute<{ affectedRows: number }>(AnnouncementQueries.AddAnnouncement, [
    [Object.values(announcement)]
  ]);
  return result.affectedRows > 0;
};

/**
 * updates announcement information based on the id provided
 */
export const updateAnnouncement = async (announcement: IAnnouncement) => {
  const result = await execute<{ affectedRows: number }>(AnnouncementQueries.UpdateAnnouncementById, [
    announcement,
    announcement.id
  ]);
  return result.affectedRows > 0;
};

/**
 * updates announcement information based on the id provided
 */
 export const deleteAnnouncement = async (id: IAnnouncement['id']) => {
  const result = await execute<{ affectedRows: number }>(AnnouncementQueries.DeleteAnnouncementById, [
    id
  ]);
  return result.affectedRows > 0;
};